package net.noday.demo.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import net.noday.demo.service.ITestService;
import net.noday.demo.service.TestService;

public class JDKProxyFactory implements InvocationHandler{

	private Object target;
	
	public Object createProxyIntance(Object target) {
		this.target = target;
		return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), 
				this.target.getClass().getInterfaces(), 
				this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		ITestService ts = (TestService) this.target;
		Object result = null;
		if (ts.getUser() != null) {
			result = method.invoke(target, args);
		}
		return result;
	}
}
